﻿/*
* 培训任务列表
* 
*/
var kdManagement = angular.module("courseNoteModel", ['tm.pagination', 'btnconqitk', 'Common', 'interlocutionService', "loadModule"])
.controller("courseNoteController", ["$scope", "$rootScope", "sysUserInfo", "interlocutionservice", "Comm", "Msg", "MsgCon", function ($scope, $rootScope, sysUserInfo, IS, Comm, Msg, MsgCon) {
    $scope.btnlist = { examineinfo: false, delinfo: false };
    $rootScope.btnTab = "index.contentAudit.CourseNote";
    //初始化
    $scope.init = function () {
        //培训任务列表集合
        $scope.List = [];

        // 选中个数
        $scope.checkAll = 0;
        //查询培训任务列表参数
        $scope.params = {
            userId: sysUserInfo.getuserinfo().user_ID,  //登录人id
            orgId: sysUserInfo.getuserinfo().organization_ID, //登录人企业id
            state:"0",
            courseName: "",    //搜索内容、（知识结构名称或拼音）
            powerLV: sysUserInfo.getuserinfo().powerLV,    // 管理端查询专家
            pageIndex: 1,   //起始下标
            pageSize: 10 //页数
        }
        $scope.getList();
    }
    //日期筛选
    $scope.GetDate = function () {
        $scope.params.start_Date = document.getElementById("startDate").value;
        $scope.params.end_Date = document.getElementById("endDate").value;
    }
    //全选
    $scope.selectAll = function () {
        $scope.selFlag = true;
        if ($scope.checkAll == $scope.List.length) { //已选
            $scope.selFlag = false;
            $scope.checkAll = 0;
        } else {
            $scope.selFlag = true;
            $scope.checkAll = $scope.List.length;
        }
        // 设置样式
        angular.forEach($scope.List, function (data, index, array) {
            data.isSel = $scope.selFlag;
        });
    }
    // 单选
    $scope.singleCheck = function (item) {
        if (item.isSel) {
            $scope.checkAll--;
            item.isSel = false;
        } else {
            $scope.checkAll++;
            item.isSel = true;
        }
    }
    /*
    *   遍历选中的对象
    *   list 集合
    *   serIds ref  id集合
    *   serItems ref obj集合不用传null
    */
    $scope.getParamsList = function (list, serIds, serItems) {
        angular.forEach(list, function (data, ind, arr) {//循环全部对象
            if (data.isSel) {// 找到选中的对象
                serIds.push(data.noteId); // 存id
                if (serItems != null)
                    serItems.push(data); // 存obj
            }
        });
    }
    $scope.selName = "全部";
    //loading 加载
    $scope.Loading = false;
    //获取列表
    $scope.getList = function () {
        $scope.Loading = true;
        IS.findToExamineNote($scope.params).then(function (response) {
            $scope.Loading = false;
            if (response.errorcode == "0") {
                $scope.List = response.datas;
                $scope.paginationConf.totalItems = response.numCount;
                $scope.paginationConf.currentPage = response.pageIndex;
            } else {
                Msg.error("获取列表失败了呦！");
            }
        }, function errorCallback() {
            console.log("连接服务器失败！");
        });
    }
    //回车事件
    $scope.findListEnter = function (e) {
        var keycode = window.event ? e.keyCode : e.which;
        if (keycode == 13) {
            $scope.getList();
        }
    }
    // 删除
    $scope.delinfo = function (item) {
        var ids = [];
        if (item == null) { // 判断是不是单个的删除
            $scope.getParamsList($scope.List, ids, null);
        } else {// 单独删除
            ids.push(item.noteId);
        }
        // 未选中
        if (Comm.isNull(ids) || ids.length <= 0) {
            Msg.warning("提示", "请选择要删除的数据!");
            return;
        }
        //提示框
        MsgCon.delnoicon().then(function (flag) {   // 删除弹框
            if (flag) {
                // 发送请求
                IS.delCourseNotes({ noteIds: ids.join(",") }).then(function (respose) {
                    if (respose.errorcode == "0") {
                        Msg.success("操作成功！");
                        $scope.checkAll = 0; // 复选框选中的数量            
                        $scope.getList();  // 重新请求一下数据
                    } else {
                        Msg.error("操作失败！");
                    }
                }, function errorCallback() {
                    console.log("连接服务器失败！");
                });
            }
        }, function (p) { console.log('error') });
    }
    //配置分页
    $scope.paginationConf = {
        currentPage: 1, //$location.search().currentPage ? $location.search().currentPage :  当前页码
        totalItems: 0,  // 总的记录数
        itemsPerPage: 10, // 每页显示多少条        
        perPageOptions: [10, 20],   // 每页显示记录数        
        onChange: function () {
            $scope.paginationConf.currentPage = $scope.paginationConf.currentPage == 0 ? 1 : $scope.paginationConf.currentPage;
            if ((parseInt($scope.params.pageIndex) != parseInt($scope.paginationConf.currentPage)) || (parseInt($scope.params.pageSize) != parseInt($scope.paginationConf.itemsPerPage))) {
                if ($scope.paginationConf.currentPage == 0) { $scope.paginationConf.currentPage = 1; }
                $scope.params.pageIndex = $scope.paginationConf.currentPage;
                $scope.params.pageSize = $scope.paginationConf.itemsPerPage;
                $scope.getList();
            }
            $scope.checkAll = 0; // 复选框选中的数量     
        }
    };
    $scope.init();

    $scope.showForm = false;
    $scope.item = {};
    $scope.oldstate = 0;//记录老的状态，关闭的时候回显
    // 关闭弹框
    $scope.closeOrOpenForm = function () {
        $scope.showForm = !$scope.showForm;
        if ($scope.showForm) {
            $scope.item.state = $scope.item.state + "";
            $scope.oldstate = $scope.item.state;
        } else {
            $scope.item.state = $scope.oldstate;
        }
    }
    //打开审核
    $scope.examine = function (item) {

        if (item != undefined) {
            $scope.item = item;
        } else {
            var count = 0;
            angular.forEach($scope.List, function (data, ind, arr) {//循环全部对象
                if (data.isSel) {// 找到选中的对象
                    $scope.item = data;
                    count++;
                }
            });
            if (count > 1) {
                Msg.warning("提示", "只能操作一条数据");
                return;
            } else if (count == 0) {
                Msg.warning("提示", "请选择一条审核数据");
                return;
            }

        }
        //打开窗口
        $scope.closeOrOpenForm();
    }
    //修改审核状态
    $scope.updateExamine = function () {
		if ($scope.item.state == 0) {
            Msg.warning("请选择审核状态！");
            return;
        }
        IS.ExamineNote({ noteId: $scope.item.noteId, examineUserName: sysUserInfo.getuserinfo().user_Name, state: $scope.item.state, userId: sysUserInfo.getuserinfo().user_ID, powerLV: sysUserInfo.getuserinfo().powerLV }).then(function (respose) {
            if (respose.errorcode == "0") {
                Msg.success("操作成功！");
                $scope.getList();
                $scope.showForm = false;
            } else {
                Msg.error("操作失败！");
            }
        }, function errorCallback() {
            console.log("连接服务器失败！");
        });
    }

    // 时间秒数格式化
    $scope.arrive_timer_format = function (s) {
        var t = "";
        if (s > -1) {
            min = parseInt(parseInt(s) / 60);
            sec = parseInt(s) % 60;
            if (min < 10) {
                t += "0";
            }
            t += min + ":";
            if (sec < 10) {
                t += "0";
            }
            t += sec;
        }
        return t;
    }
} ]);
